/*
 * NamespaceAttr.java
 *
 * Created on September 30, 2006, 1:27 PM
 *
 * (C) 2006 R. Alexander Milowski
 */

package org.infoset.xml.dom;

import java.net.URI;
import org.infoset.xml.Name;
import org.w3c.dom.Attr;
import org.w3c.dom.DOMException;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.TypeInfo;

/**
 *
 * @author alex
 */
public class NamespaceAttr implements Attr {
   
   DOMElementProxy owner;
   String namespace;
   String localName;
   boolean defaultDecl;
   /** Creates a new instance of NamespaceAttr */
   private NamespaceAttr(DOMElementProxy owner, String localName, String uri) {
      this.owner = owner;
      this.defaultDecl = Name.NO_PREFIX.equals(localName);
      this.localName = defaultDecl ? "xmlns" : localName;
      this.namespace = uri;
   }
   public NamespaceAttr(DOMElementProxy owner, String localName, URI uri) {
      this.owner = owner;
      this.defaultDecl = Name.NO_PREFIX.equals(localName);
      this.localName = defaultDecl ? "xmlns" : localName;
      this.namespace = uri==Name.NO_NAMESPACE ? "" : uri.toString();
   }
   
   public boolean isId() {
      return false;
   }
   
   public TypeInfo getSchemaTypeInfo() {
      return null;
   }
   
   public org.w3c.dom.Element getOwnerElement() {
      return owner;
   }
   
   public void setValue(String value) {
      throw new UnsupportedOperationException("Cannot change namespace attributes through the DOM proxy to the infoset.");
   }
   
   public String getValue() {
      return namespace;
   }
   
   public String getTextContent() {
      return namespace;
   }
   
   public boolean getSpecified() {
      return true;
   }
   
   public String getName() {
      return defaultDecl ? localName : "xmlns:"+localName;
   }
   
   public String getNodeName() {
      return getName();
   }
   
   public Object getUserData(String key) {
      return null;
   }
   public Object setUserData(String key,Object value,org.w3c.dom.UserDataHandler handler) {
      throw new UnsupportedOperationException("Cannot set user data on the namespace node DOM proxy to the infoset.");
   }
   public Object getFeature(String feature,String version) {
      return null;
   }
   
   public boolean isSupported(String feature,String version) {
      return false;
   }
   
   public boolean isEqualNode(org.w3c.dom.Node other) {
      return false;
   }
   public String lookupNamespaceURI(String prefix) {
      return owner.lookupNamespaceURI(prefix);
   }

   public String lookupPrefix(String namespaceURI) {
      return owner.lookupPrefix(namespaceURI);
   }
   
   public boolean isDefaultNamespace(String namespaceURI) {
      return owner.isDefaultNamespace(namespaceURI);
   }
   
   public boolean isSameNode(org.w3c.dom.Node node) {
      return node==this;
   }
   
   public void setTextContent(String value) {
      setValue(value);
   }
   
   public short compareDocumentPosition(Node other) {
      throw new DOMException(DOMException.NOT_SUPPORTED_ERR,"Comparing document position is not supported.");
   }
   
   public String getBaseURI() {
      return owner.getBaseURI();
   }
   
   public boolean hasAttributes() {
      return false;
   }
   public NamedNodeMap getAttributes() {
      return null;
   }
   
   public String getLocalName() {
      return localName;
   }
   
   public void setPrefix(String prefix) {
      throw new DOMException(DOMException.NOT_SUPPORTED_ERR,"Cannot set the prefix on a namespace attribute.");
   }
   
   public String getPrefix()
   {
      return defaultDecl ? "" : "xmlns";
   }
   
   public String getNamespaceURI() {
      return defaultDecl ? "" : DOMElementProxy.XMLNS_NAMESPACE;
   }
   
   public void normalize() {
   }
   
   public Node cloneNode(boolean deep) {
      return new NamespaceAttr(owner,localName,namespace);
   }
   
   public boolean hasChildNodes() {
      return false;
   }
   
   public Node appendChild(Node n) {
      throw new DOMException(DOMException.NOT_SUPPORTED_ERR,"Method not supported on attributes.");
   }
   public Node removeChild(Node n) {
      throw new DOMException(DOMException.NOT_SUPPORTED_ERR,"Method not supported on attributes.");
   }
   public Node insertBefore(Node newNode,Node refChild) {
      throw new DOMException(DOMException.NOT_SUPPORTED_ERR,"Method not supported on attributes.");
   }
   public Node replaceChild(Node newNode,Node oldChild) {
      throw new DOMException(DOMException.NOT_SUPPORTED_ERR,"Method not supported on attributes.");
   }
   
   public Node getNextSibling() {
      return null;
   }
   public Node getPreviousSibling() {
      return null;
   }
   public Node getLastChild() {
      return null;
   }
   public Node getFirstChild() {
      return null;
   }
   public NodeList getChildNodes() {
      return null;
   }
   public Node getParentNode() {
      return null;
   }
   
   public org.w3c.dom.Document getOwnerDocument() {
      return owner.getOwnerDocument();
   }
   
   public short getNodeType() {
      return Node.ATTRIBUTE_NODE;
   }
   
   public String getNodeValue() {
      return getTextContent();
   }
   
   public void setNodeValue(String value) {
      setValue(value);
   }
}
